CounterVar is a short (140 bytes) utility to create an OS variable which acts as a counter. Every time this variable is read, its value is automatically incremented. It is designed to be replace parts of code that currently read, increment and write a conventional environment variable (eg the UnixLib pipe counter)
The utility is able to create any number of counter variables - call it once for each counter to create. The name of the variable to create is supplied as the first parameter, the optional second parameter is the initial value (defaults to zero)
Syntax: CounterVar <Name> [<Initial Value>]
CounterHex counts in hexadecimal rather than decimal (and always generates exactly 8 character output.)
The counter variable is implemented as a code variable - the advantages of this method over reading, modifying and writing a conventional variable from inside your code are:
0: The code variable method is almost certainly more compact.
1: This method will be faster - only one SWI call to read the variable, none to
write it back
2: [As long as you aren't trying to read the counter under interrupts] you can't
accidentally read the same value twice.
Using a conventional variable, two programs running in task windows can, if
one is unlucky enough to read the value just after a second program has read
it, but just before the second program writes back the incremented value.
Currently you can write back to the variable to set the counter to that value - I'm not sure if this is a good thing, but it does allow programs that are not aware of the auto-increment to treat this variable as if it were nothing special.
Hopefully the source serves as an example of how to write a transient utility, and how to write a code variable.
I would prefer it it if you distribute the source with the executable
Feel free to use this in anything free. Feel free to modify/improve it.
Feel obliged to send me any bug fixes at <nickc@done.net>